/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number[]} to_delete
 * @return {TreeNode[]}
 */
var delNodes = function (root, to_delete) {
  let set = new Set(to_delete), treeArr = []

  function dfs(node) {
    if (!root) return
    if (set.has(node.val)) {
      if (!node.left && !node.right) {
        node = null
      } else {
        if (node.left) {
          treeArr.push(node.left)
        }
        if (node.right) {
          treeArr.push(node.right)
        }
      }
    } else {
      dfs(node.left)
      dfs(node.right)
    }

  }
  dfs(root)
  return treeArr.push(root)
};